const app = getApp()
import { requestApi } from '../../api'
import { formatImage } from '../../utils/formatImage';
import { formatPrice } from '../../utils/util'

Page({
    data: {
        brand_info: {},
        isMore: false,
        goodsList: [],              // 商品瀑布流数据
        requestFlag: false,         // 加载瀑布流的请求标识
        page: 1,                    // 初始加载第一页数据
        noMore: false,
        brandId: '',
        sellerUid: '',
        channel: '',
        isShowHide: false,
        sysWidth: 0,
        sysHeight: 0
    },
    onReady() {
        this.setData({
            sysWidth: wx.getSystemInfoSync().windowWidth,
            sysHeight: wx.getSystemInfoSync().windowHeight
        })
    },
    onLoad:function(opt) {
        let channel = opt && opt.channel || 'wx_xcx';
        this.setData({
            brand_id: opt.brand_id,
            seller_uid: opt.seller_uid,
            channel
        });
    },
    onShow: function () {
        const { scene } = wx.getLaunchOptionsSync();
        const { brand_id, seller_uid, channel } = this.data;

        if (scene == 1047 && !app.toLogin()) {
            this.setData({
                loading: true
            })
            return false
        } else if (brand_id) {
            this.getBrandInfo(brand_id);
            // 请求 热门商品 数据
            this.setData({
                brandId: brand_id,
                sellerUid: seller_uid,
                channel: channel,
            })

            this.getGoodsData(this.data.page, channel);
            if(scene == 1047) {
                this.isFlashStore(seller_uid);
            }
        }
    },
    getBrandInfo(id) {
        app.request({
            url: requestApi.wxBrandInfo,
            login: false,
            data: {
                brand_id: id
            },
        }, (data) => {
            let d = data.original;
            if (d.status == 'success') {
                if (d.data.brand_info) {
                    d.data.brand_info.brand_detail_top_image.path_full = formatImage.cdnImage(d.data.brand_info.brand_detail_top_image.path);

                    if (d.data.brand_info.description.length > 100) {
                        d.data.brand_info['des'] = d.data.brand_info.description.substring(0, 50) + '...';
                        this.setData({
                            isShowHide: true
                        });
                    } else {
                        d.data.brand_info['des'] = d.data.brand_info.description;
                    }

                    this.setData({
                        brand_info: d.data.brand_info
                    });
                }
            }
        })
    },
    showHide() {
        let { isMore } = this.data;

        this.setData({
            isMore: !isMore
        })
    },
    isFlashStore: function (seller_uid) {
        app.request({
            url: requestApi.isFlashStore,
            login: false,
            data: {
                seller_uid: seller_uid
            },
        }, (data) => {
            if(data.original.data.is_flash) {
                this.setUserFlashName(data.original.data.shop_tag);
            }
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        })
    },
    setUserFlashName(shop_name) {
        app.request({
            url: requestApi.setUserFlashName,
            login: false,
            data: {
                flash_shop_tag: shop_name
            },
        }, (data) => {
            let d = data.original;
            if (d.status == 'success') {
      
            }
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        })
    },
    /**
  * 事件处理
  * 热门商品上拉加载更多
  */
    scrolltolower: function (e) {
        this.getGoodsData(this.data.page, this.data.channel)
    },
    /**
  * 获取商品数据
  */
    getGoodsData: function (page, channel) {
        let { noMore, requestFlag, brandId, sellerUid, goodsList } = this.data;

        if (noMore) return false
        if (requestFlag) return false
        this.setData({
            requestFlag: true,
        })

        let count = 20;
        let start = (page - 1) * count;
        app.request({
            url: requestApi.goodsUrl,
            login: false,
            data: {
                start: start,
                count: count,
                channel: channel,
                brand_id: brandId,
                seller_uid: sellerUid,
            },
        }, (data) => {
            let goodsData, currentData, noMore, page_count

            currentData = goodsList;
            goodsData = data.original.data.goods.goods_list;
            goodsData = this.formatData(goodsData);
            page_count = goodsData.length ? goodsData.length : 0
            goodsData = currentData.concat(goodsData);

            if (page_count < count) {
                noMore = true
            } else {
                noMore = false
            }

            this.setData({
                goodsList: goodsData,
                loading: true,
                page: this.data.page + 1,
                noMore: noMore,
            })
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        }, () => {
            this.setData({
                requestFlag: false,
            })
        })
    },
    goToGoods: function (e) {
        var id = e.currentTarget.id
        let url = '/pages/goods/goodsDetail/goodsDetail?gid=' + id + '&channel=' + this.data.channel
        wx.navigateTo({
            url: url,
        })
    },

    // 处理请求返回的数据
    formatData: function (data) {
        if (data.length) {
            var goods_channel_source_c = ['国内现货', '海外现货', '代购' ];

            for (let i = 0, len = data.length; i < len; i++) {
                if (data[i].product_cover_image && data[i].product_cover_image.path) {
                    data[i].product_cover_image.new_path = formatImage.cdnImage(data[i].product_cover_image.path, formatImage.THUMBNAIL_S)
                }

                if (data[i].goods_status_icon && data[i].goods_status_icon.path) {
                    data[i].goods_status_icon.full_path = formatImage.cdnImage(data[i].goods_status_icon.path, formatImage.THUMBNAIL_S)
                }

                data[i].new_goods_price = data[i].goods_price ? formatPrice(data[i].goods_price / 100) : ''
                data[i].original_price = data[i].original_price ? formatPrice(data[i].original_price / 100) : ''
                data[i].goods_price = data[i].goods_price ? formatPrice(data[i].goods_price / 100) : ''
                data[i].goods_channel_source_c = goods_channel_source_c[data[i].goods_channel_source];
            }
        }

        return data
    },
})